package com.example.hidesms;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import com.example.hidesms.bo.BlockSmsBO;
import com.example.hidesms.dto.BlockSmsItem;
import com.example.hidesms.sqlite.DataBaseHelper;
import com.example.hidesms.utils.Utils;

import android.content.res.Resources.Theme;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

public class AddBlockSmsNumberDialog extends DialogFragment{
	private EditText mEditText;
	private TextView tvOk, tvNo;
	public AddBlockSmsNumberDialog() {
        // Empty constructor required for DialogFragment
    }

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.add_block_dialog, container);
        getDialog().setCanceledOnTouchOutside(true);
        getElementFromUI(view);

        return view;
    }
   
    
    private void getElementFromUI(View view){
    	mEditText = (EditText) view.findViewById(R.id.edtNumber);
    	tvOk= (TextView) view.findViewById(R.id.tvOK);
    	tvNo = (TextView) view.findViewById(R.id.tvNo);
    	tvOk.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				BlockSmsItem bi  = new BlockSmsItem();
				String name = mEditText.getText().toString().trim();
				String[] data= Utils.getContactByNumber(name, getActivity());
				DataBaseHelper myDatabase= new DataBaseHelper(getActivity());
				Uri photoUri = null;
				if (data!=null) {
					bi.setName(data[0]);
					if (data[1]!=null) {
						photoUri = Uri.parse(data[1]);
					}
				}else{
					bi.setName(name);
				}
				bi.setNumber(name);
				if (photoUri!=null) {
					InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(getActivity()
							.getContentResolver(), photoUri);
					
					Bitmap bm = BitmapFactory.decodeStream(input);
					if (bm != null) {
						ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
						if (bm.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream)) {
							bi.setImage(byteArrayOutputStream.toByteArray());
						}
					}
				}
				
				
				BlockSmsBO.getInstance().insert(myDatabase, bi);
				dismiss();
			}
		});
    	tvNo.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dismiss();
			}
		});
    	
    }
    public static AddBlockSmsNumberDialog newInstance() {
    	AddBlockSmsNumberDialog mDialogFragment = new AddBlockSmsNumberDialog();
        //Set Arguments here if needed for dialog auto recreation on screen rotation
        mDialogFragment.setStyle(DialogFragment.STYLE_NO_TITLE, DialogFragment.STYLE_NO_FRAME);
        return mDialogFragment;
}


}
